<template>
	<view>
		<u-tabbar :value="value4" @change="transto" :fixed="true" :border="false" :placeholder="true" activeColor="red" :safeAreaInsetBottom="true">
			<!-- 不能在这儿使用tap事件，他触发的不是得到下标的 -->
			<u-tabbar-item :text="item.text" :icon="item.icon" :key="index" v-for="(item, index) in list"></u-tabbar-item>
		</u-tabbar>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
	computed: {
		// ...mapState(['value4'])
		value4() {
			return this.$store.state.tabindex;
		}
	},
	data() {
		return {
			// value4: 0,
			//存高亮页面
			activePage:'',
			list: [
				{
					text: '首页',
					pagePath: '/pages/index/index',
					icon: 'account'
				},
				{
					text: '专题',
					pagePath: '/pages/topic/topic',
					icon: 'account'
				},
				{
					text: '分类',
					pagePath: '/pages/category/category',
					icon: 'photo'
				},
				{
					text: '购物车',
					pagePath: '/pages/cart/cart',
					icon: 'play-right'
				},
				{
					text: '我的',
					pagePath: '/pages/user/user',
					icon: 'account'
				}
			]
		};
	},
	
	methods: {
		...mapMutations(['changevalue', 'changetabpage']),
		transto(e) {
			console.log(e,this.activePage);
			// console.log(this.list);
			// console.log(this.$route.path=='/pages/user/user')
		
			if (e != 3) {
				uni.switchTab({
					url: this.list[e].pagePath
				});
				this.changevalue(e);
				// this.activePage=this.list[e].text
				// console.log(this.activePage)
			} else 	if(e==3&&this.$route.path=='/pages/user/user'){
				//如果高亮页面为我的
				console.log(this.activePage)
				console.log('显示请先登录')
				uni.showModal({
					title: '未登录，请先登录',
				})
			}else {
				
				let token = uni.getStorageSync('token');
				if (!token) {
					uni.showModal({
						title: '未登录，是否跳转登录页?',
						// content: '是否跳转？'
						success: res => {
							console.log(res);
							if (res.cancel) {
								// 取消
								console.log('不跳转');
							} else if (res.confirm) {
								// 确定
								console.log('到登录页面');
								// 保存当前页面的url
								let url = getCurrentPages();
								console.log('/' + url[0].route);
								this.changetabpage('/' + url[0].route);
								uni.switchTab({
									url: this.list[4].pagePath
								});
								this.changevalue(4);
								
							// this.activePage=this.list[e].text
							// console.log(this.activePage)
							}
						}
					});
				} else {
					uni.switchTab({
						url: this.list[3].pagePath
					});
					this.changevalue(3);
					
					// this.activePage=this.list[e].text
					// console.log(this.activePage)
				}
			}
		}
	}
};
</script>

<style></style>
